package com.devin.dao;

import com.devin.pojo.Role;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cache.decorators.LruCache;
import org.omg.PortableInterceptor.INACTIVE;

import java.util.List;

@Mapper
@CacheNamespace(blocking = true,eviction = LruCache.class,flushInterval=0L,size=1024,readWrite = true)
public interface RoleMapper {
    @Select(" select * from role")
    @Options(useCache = true)
    List<Role> getRoles();

    @Select(" select * from role where id in (select roleId from users_role where userId=#{userId})")
    @Options(useCache = true)
    List<Role> getRolesByUserId(Integer userId);

    @Select("select * from role where id not in (select roleId from users_role where userId=#{userId})")
    @Options(useCache = true)
    List<Role> getRoles2Json(Integer userId);

    @Insert(" insert into role values(null,#{roleName},#{roleDesc})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void addRole(Role role);

    @Select(" select * from role where id=#{roleId}")
    @Options(useCache = true)
    @Results({@Result(id = true,property = "id",column = "id")
            ,@Result(property = "permissions",column = "id",javaType = List.class,many = @Many(select = "com.devin.dao.PermissionMapper.getPermissionsByRoleId"))
    })
    Role getRoleById(Integer roleId);

    @Delete(" delete from role_permission where roleId=#{roleId} and permissionId=#{permissionId}")
    void delPermissionRoleId(@Param("roleId") Integer roleId, @Param("permissionId") Integer permissionId);

    @Delete("delete from role_permission where roleId=#{roleId}")
    void delRoleAndPermissions(Integer roleId);

    @Delete(" delete from role where id =#{roleId}")
    void delRole(Integer roleId);
}
